package view.backing.Utils;

import chemtools_logica.LN.IR.LN_SFBancoRemote;
import chemtools_logica.LN.IR.LN_SFGastoRemote;
import chemtools_logica.LN.IR.LN_SFTipogastoRemote;
import chemtools_logica.LN.IR.LN_SFUbigeoRemote;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Ubigeo {
    private LN_SFUbigeoRemote lN_SFUbigeoRemote;
    
    public Ubigeo(){
        try {
            final Context context = getInitialContext();
            lN_SFUbigeoRemote = (LN_SFUbigeoRemote) context.lookup("map-LN_SFUbigeo#chemtools_logica.LN.IR.LN_SFUbigeoRemote");
        }catch(Exception e){}
    }
    
    public ArrayList llenarDepartamentos() {
        ArrayList departamentos = new ArrayList();
        List<chemtools_logica.Entidades.Ubigeo> lista = lN_SFUbigeoRemote.getAllDepartamentos();
        for(chemtools_logica.Entidades.Ubigeo ent : lista){
            departamentos.add(new SelectItem(ent.getCod_dept(),ent.getDepartamento()));
        }
        return departamentos;
    }
    
    public void llenarProvincias(ValueChangeEvent valueChangeEvent){
        String idDepartamento = valueChangeEvent.getNewValue().toString();
        System.out.println("ID: "+idDepartamento);
        //List<chemtools_logica.Entidades.Ubigeo> lista = lN_SFUbigeoRemote.getProvincias(idDepartamento);
    }
    
    public ArrayList llenarProvincias(String idDepartamento){
        ArrayList provincias = new ArrayList();
        List<chemtools_logica.Entidades.Ubigeo> lista = lN_SFUbigeoRemote.getProvincias(idDepartamento);
        for(chemtools_logica.Entidades.Ubigeo ent : lista){
            provincias.add(new SelectItem(ent.getCod_prov(),ent.getProvincia()));
        }
        return provincias;
    }
    
    public ArrayList llenarDistritos(String idProvincia){
        ArrayList distritos = new ArrayList();
        List<chemtools_logica.Entidades.Ubigeo> lista = lN_SFUbigeoRemote.getDistritos(idProvincia);
        for(chemtools_logica.Entidades.Ubigeo ent : lista){
            distritos.add(new SelectItem(ent.getCod_dist(),ent.getDistrito()));
        }
        return distritos;
    }
    
    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x/12.x connection details
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext(env);
    }
}
